package com.junlong0716.enjoylearning.ui

import android.support.v4.content.ContextCompat
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import com.blankj.utilcode.util.ConvertUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.junlong0716.enjoylearning.R
import com.junlong0716.enjoylearning.adapter.KnowledgeListAdapter
import com.junlong0716.enjoylearning.entity.KnowledgeListEntity
import com.junlong0716.enjoylearning.entity.rxevent.ChangeTabEvent
import com.junlong0716.getknowledgedemo.decoration.DividerItemNoEndDecoration
import com.junlong0716.module.common.base.BaseFragment
import com.junlong0716.module.common.rxbus.RxBus
import com.scwang.smartrefresh.layout.SmartRefreshLayout

/**
 *@author: 巴黎没有摩天轮Li
 *@description:
 *@date: Created in 下午9:21 2018/5/11
 *@modified by:
 */
class KnowledgeListFragment : BaseFragment<KnowledgeListPresenter>(), KnowledgeListContract.View, BaseQuickAdapter.RequestLoadMoreListener {
    private lateinit var knowList: ArrayList<KnowledgeListEntity>
    private lateinit var knowledgeListAdapter: KnowledgeListAdapter
    private lateinit var srfLayout: SmartRefreshLayout
    override fun getLayoutId(): Int = R.layout.fragment_knowledge_list

    override fun initViews(mRootView: View?) {
        srfLayout = mRootView!!.findViewById<SmartRefreshLayout>(R.id.srf_layout)
        srfLayout.isEnableLoadMore = false

        val rvList = mRootView.findViewById<RecyclerView>(R.id.rv_list)
        initData()
        rvList.layoutManager = LinearLayoutManager(activity)
        knowledgeListAdapter = KnowledgeListAdapter(knowList, R.layout.item_knowledge_list)
        rvList.adapter = knowledgeListAdapter
        rvList.addItemDecoration(DividerItemNoEndDecoration(activity!!, DividerItemNoEndDecoration.VERTICAL_LIST, ConvertUtils.dp2px(0.5f), ContextCompat.getColor(activity!!, R.color.divider_gray)))
        knowledgeListAdapter.setOnLoadMoreListener(this, rvList)
        knowledgeListAdapter.addFooterView(LayoutInflater.from(activity!!).inflate(R.layout.view_knowledge_list_footer, null))
        knowledgeListAdapter.setOnItemClickListener { adapter, view, position ->
            when (position) {
                0 -> {
                    go(StudyHistoryActivity::class.java)
                }
                1 -> {
                    //go(StudyCollectionActivity::class.java)
                    go(NoteActivity::class.java)
                    //RxBus.getDefault().post(ChangeTabEvent(0xfafb))
                }
                2 -> {
                    //(fra as StudyFragment).changeTab()
                    //go(StudyNoteActivity::class.java)
                    //(this.parentFragment as StudyFragment).changeTab()
                    //go(StudyMessageActivity::class.java)
                }
                3 -> {

                }
            }

        }


        srfLayout.setOnRefreshListener {
            mPresenter!!.requestKnowledgeCount(this)
        }
    }

    private fun initData() {
        knowList = ArrayList()
        knowList.add(KnowledgeListEntity(R.mipmap.ic_knowledge_list_historical_record, "历史记录", 0))
        //knowList.add(KnowledgeListEntity(R.mipmap.ic_knowledge_list_collection, "我的收藏", 0))
        knowList.add(KnowledgeListEntity(R.mipmap.ic_knowledge_list_notes, "我的笔记", 0))
       // knowList.add(KnowledgeListEntity(R.mipmap.ic_knowledge_list_message, "文章留言", 0))
    }

    override fun lazyFetchData() {
        srfLayout.autoRefresh()
    }

    override fun attachPresenter() {
        mPresenter = KnowledgeListPresenter()
        mPresenter!!.attachView(this)
    }

    override fun requestKnowledgeCountSucceed(collectCount: Int, noteQueryCount: Int, learnRecordCount: Int, commentCount: Int) {
        knowList.clear()


        knowList.add(KnowledgeListEntity(R.mipmap.ic_knowledge_list_historical_record, "历史记录", learnRecordCount))
       // knowList.add(KnowledgeListEntity(R.mipmap.ic_knowledge_list_collection, "我的收藏", collectCount))
        knowList.add(KnowledgeListEntity(R.mipmap.ic_knowledge_list_notes, "我的笔记", noteQueryCount))
      //  knowList.add(KnowledgeListEntity(R.mipmap.ic_knowledge_list_message, "文章留言", commentCount))
        knowledgeListAdapter.notifyDataSetChanged()
        srfLayout.finishRefresh()
    }

    override fun onLoadMoreRequested() {
        knowledgeListAdapter.loadMoreEnd(true)
    }
}